
import { Service } from 'egg';

export default class Tools extends Service {
    /**
    * 获取微信文章wxv地址
    * */
    public async getMpWxv() {
        const { ctx, app } = this;
        const { Joi } = app;

        let schema = Joi.object({
            url: Joi.string().required(),
        });
        let { error } = ctx.validate(schema, ctx.request.body);
        if (error) {
            ctx.status = 403;
            return { error: error.message };
        };
        let res = await ctx.curl(ctx.request.body.url, {
            method: 'GET',
            headers: {
                'user-agent': ctx.headers['user-agent'],
                'origin': ctx.headers['origin']
            }
        })
        try {

            let content = Buffer.from(res.data).toString('utf-8');
            let match = content.match(/wxv_(\d+)/)
            return {
                code: 200,
                data: (match && match.length) ? match[0] : null
            }
        } catch (error) {
            return {
                code: 100
            }
        }
    }
}